// 课程销量统计
fetch('/api/stats/course-sales')
    .then(response => response.json())
    .then(data => {
      var ctx = document.getElementById("myBarChart");
      new Chart(ctx, {
        type: 'bar',
        data: {
          labels: data.labels,
          datasets: [{
            label: "课程报名人数",
            backgroundColor: "rgba(75, 192, 192, 0.6)",
            borderColor: "rgba(75, 192, 192, 1)",
            borderWidth: 1,
            data: data.data,
          }],
        },
        options: {
          scales: {
            xAxes: [{
              gridLines: {
                display: false
              },
              ticks: {
                autoSkip: false
              }
            }],
            yAxes: [{
              ticks: {
                beginAtZero: true,
                stepSize: 3,
                max: Math.max(...data.data) + (3 - Math.max(...data.data) % 3), // 向上取整到最近的3的倍数
                min: 0
              },
              gridLines: {
                display: true
              }
            }],
          },
          legend: {
            display: true,
            position: 'top'
          },
          title: {
            display: true,
            text: '课程报名人数统计'
          }
        }
      });
    })
    .catch(error => console.error('Error:', error));